//
//  HJEmojiView.m
//  KaiChat
//
//  Created by lx on 2023/11/17.
//

#import "HJEmojiView.h"
#import "HJEmojiCell.h"
#import "HJFaceCellData.h"
#import "TUIDefine.h"
#import "HJCommonModel.h"

 
@interface HJEmojiView () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSArray <NSString *> *datas;
 

@end

@implementation HJEmojiView

- (id)initWithFrame:(CGRect)frame {
    if(self = [super initWithFrame:frame]){
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    self.backgroundColor = UIColor.whiteColor;

    _faceFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    _faceFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _faceFlowLayout.sectionInset = UIEdgeInsetsMake(TFaceView_Margin, TFaceView_Page_Padding, Bottom_SafeHeight, TFaceView_Page_Padding);
    _faceFlowLayout.itemSize = CGSizeMake(30, 30);

    NSInteger count = 8;
    _faceFlowLayout.minimumLineSpacing = (KScreenWidth - _faceFlowLayout.sectionInset.left - _faceFlowLayout.sectionInset.right - count * _faceFlowLayout.itemSize.width) / (count - 1);;
    _faceFlowLayout.minimumInteritemSpacing =  _faceFlowLayout.minimumLineSpacing;

    _faceCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_faceFlowLayout];
    [_faceCollectionView registerClass:[HJEmojiCell class] forCellWithReuseIdentifier:@"HJEmojiCell"];
    _faceCollectionView.collectionViewLayout = _faceFlowLayout;
    _faceCollectionView.delegate = self;
 
    
    _faceCollectionView.dataSource = self;
    _faceCollectionView.showsHorizontalScrollIndicator = NO;
    _faceCollectionView.backgroundColor = self.backgroundColor;
    _faceCollectionView.alwaysBounceVertical = YES;
    [self addSubview:_faceCollectionView];

    _lineView = [[UIView alloc] init];
    _lineView.backgroundColor = UIColorMake(0xDBDBDB);
 }

- (void)didClickSendButton {
    if ([self.delegate respondsToSelector:@selector(emojiViewDidSend:)]) {
        [self.delegate emojiViewDidSend:self];
    }
    
}

- (void)didClickDeleteButton {
    if ([self.delegate respondsToSelector:@selector(emojiViewDidBackDelete:)]) {
        [self.delegate emojiViewDidBackDelete:self];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    _lineView.frame = CGRectMake(0, 0, self.frame.size.width, TLine_Heigh);
    _faceCollectionView.frame = CGRectMake(0, _lineView.frame.origin.y + _lineView.frame.size.height, self.frame.size.width, self.frame.size.height  - _lineView.frame.size.height);
}
 
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HJEmojiCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HJEmojiCell" forIndexPath:indexPath];
    
    NSString *emoji = self.datas[indexPath.row];
    cell.emojiLabel.text = emoji;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *emoji = self.datas[indexPath.row];
    if(_delegate && [_delegate respondsToSelector:@selector(emojiView:didSelectFaceCellWithEmoji:)]){
        [_delegate emojiView:self didSelectFaceCellWithEmoji:emoji];
    }
 
}
 
- (NSArray<NSString *> *)datas {
    if (!_datas) {
        _datas = @[@"😀", @"😃", @"😄", @"😁", @"😆", @"😅", @"😂", @"🤣", @"☺", @"😊", @"😇", @"🙂", @"🙃", @"😉", @"😌", @"😍", @"🥰", @"😘", @"😗", @"😙", @"😚", @"😋", @"😛", @"😝", @"😜", @"🤪", @"🤨", @"🧐", @"🤓", @"😎", @"🤩", @"🥳", @"😏", @"😒", @"😞", @"😔", @"😟", @"😕", @"🙁", @"☹", @"😣", @"😖", @"😫", @"😩", @"🥺", @"😢", @"😭", @"😤", @"😠", @"😡", @"🤬", @"🤯", @"😳", @"🥵", @"🥶", @"😱", @"😨", @"😰", @"😥", @"😓", @"🤗", @"🤔", @"🤭", @"🤫", @"🤥", @"😶", @"😐", @"😑", @"😬", @"🙄", @"😯", @"😦", @"😧", @"😮", @"😲", @"🥱", @"😴", @"🤤", @"😪", @"😵", @"🤐", @"🥴", @"🤢", @"🤮", @"🤧", @"😷", @"🤒", @"🤕", @"🤑", @"🤠", @"😈", @"👿", @"👹", @"👺", @"🤡", @"💩", @"👻", @"💀", @"☠", @"👽", @"👾", @"🤖", @"🎃", @"😺", @"😸", @"😹", @"😻", @"😼", @"😽", @"🙀", @"😿", @"😾"];
    }
    return _datas;
}
 
 
@end
